package cn.rubintry.cardtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnStartRead;
    private Button btnStopRead;
    private Button btnClear;

    private StringBuffer logBuffer;
    private TextView tvLog;
    private ScrollView slLog;
    /**
     * 行号
     */
    private int lineIndex;

    private Handler handler;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "ip: " + NetUtils.GetNetIp());
            }
        }).start();
        handler = new Handler();

        lineIndex = 0;
        logBuffer = new StringBuffer();

        btnStartRead = findViewById(R.id.btnStartRead);
        btnStopRead = findViewById(R.id.btnStopRead);
        btnClear = findViewById(R.id.btnClear);
        tvLog = findViewById(R.id.tvLog);
        slLog = findViewById(R.id.slLog);

        btnStartRead.setOnClickListener(this);
        btnStopRead.setOnClickListener(this);
        btnClear.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStartRead:
                IcCardReadUtils.getInstance().init(this);
                IcCardReadUtils.getInstance().setCallback(new IcCardReadUtils.Callback() {
                    @Override
                    public void startReadCard() {
                        lineIndex++;
                        logBuffer.append(lineIndex).append(".").append("正在读取").append("\n");
                        tvLog.setText(logBuffer.toString());


                    }

                    @Override
                    public void onSuccess(boolean isIdCard, String cardId, String cardNum, int errorCode1, int errorCode2, int errorCode3, String name) {
                        if (isIdCard) {
                            logBuffer.append("读取成功，卡号：").append(cardNum).append("    卡片为身份证").append("\n\n");
                        } else {
                            logBuffer.append("读取成功，卡号：").append(cardNum).append("    卡片为IC卡").append("\n\n");
                        }

                        tvLog.setText(logBuffer.toString());

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                slLog.fullScroll(ScrollView.FOCUS_DOWN);
                            }
                        });
                    }

                    @Override
                    public void onFail() {
                        logBuffer.append("读取失败").append("\n\n");
                        tvLog.setText(logBuffer.toString());

                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                slLog.fullScroll(ScrollView.FOCUS_DOWN);
                            }
                        });
                    }
                });
                IcCardReadUtils.getInstance().startRead();
                break;
            case R.id.btnStopRead:
                IcCardReadUtils.getInstance().release();
                break;
            case R.id.btnClear:
                logBuffer = new StringBuffer();
                tvLog.setText("");
                lineIndex = 0;
                break;

            default:
                break;
        }
    }


}
